package com.wuming.house.service.impl;

import com.wuming.house.dao.GoodGroupMapper;
import com.wuming.house.model.GoodsGroup;
import com.wuming.house.service.GoodsGroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhuai
 * @createTime 2019/8/19 8:46
 * @Description:
 */
@Service("goodGroupService")
public class GoodGroupServiceImpl extends BaseServiceImpl implements GoodsGroupService {

    @Autowired
    private GoodGroupMapper goodGroupMapper;

    @Override
    public List<GoodsGroup> getGoodsGroupByPage(String name, int pageIndex,int pageSize) {
        Map<String,Object> params = new HashMap<>();
        try {
            if (StringUtils.isNotBlank(name)){
                params.put("name",name);
            }
            return (List<GoodsGroup>) this.getPageList(GoodGroupMapper.class,"selectGoodsGroups",params,pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getTotal(String name) {
        Map<String,Object> params = new HashMap<>();
        try {
            if (StringUtils.isNotBlank(name)){
                params.put("name",name);
            }
            return this.getTotal(GoodGroupMapper.class,"selectTotal",params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public GoodsGroup getGoodsGroupById(Integer id) {
        try {
            return goodGroupMapper.selectGoodsGroupById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void saveGoodsGroup(GoodsGroup goodsGroup) {
        try {
            goodGroupMapper.insert(goodsGroup);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库插入分组失败,失败信息:{}", e.getMessage());
        }
    }

    @Override
    public void modifyGoodsGroup(GoodsGroup goodsGroup) {
        try {
            goodGroupMapper.update(goodsGroup);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库更新分组失败,失败原因:{}", e.getMessage());
        }
    }

    @Override
    public void deleteGoodsGroupById(Integer id) {
        try {
            goodGroupMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库删除分组失败,失败原因:{}", e.getMessage());
        }
    }

    @Override
    public GoodsGroup getGoodsGroupByType(Integer groupType) {
        Map<String, Object> params = new HashMap<>();
        params.put("groupType", groupType);
        List<GoodsGroup> goodsGroups = goodGroupMapper.selectGoodsGroups(params);
        if (null != goodsGroups && !goodsGroups.isEmpty()) {
            return goodsGroups.get(0);
        }
        return null;
    }
}
